//
//  BaseCollectionViewController.swift
//  QingLiuSwift
//
//  Created by xyanl on 2024/1/2.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import UIKit
import SnapKit
import Reusable

// 补位 covering
// 交替 alternation
class BaseCollectionViewController: BaseViewController {

    var layout = YLCollectionViewWaterfallLayout()
    lazy var collectionView: UICollectionView = {
//        // 异步刷新数据
//        waterfallLayout.asyncUpdateLayout(itemTotal: models.count) { [weak self] index, itemWidth in
//            guard let self = self else { return 1 }
//            let model = self.models[index]
//            return itemWidth * (model.image.size.height / model.image.size.width)
//        } completion: { [weak self] in
//            guard let self = self else { return }
//            UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.88, initialSpringVelocity: 1) {
//                self.collectionView.performBatchUpdates {
//                    self.collectionView.reloadSections(IndexSet(integer: 0))
//                }
//            }
//        }
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.backgroundColor = .clear
        collectionView.showsVerticalScrollIndicator = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.alwaysBounceVertical = true
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "UICollectionViewCell")
        // collectionView.register(cellType: GeneralImageCenterCollectionCell.self)
        
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        
    }
    
}

extension BaseCollectionViewController {
    // 动画更新
    func animateUpdate(_ updateHandle: @escaping (_ collectionView: UICollectionView) -> Void) {
        UIView.animate(
            withDuration: 1, 
            delay: 0, 
            usingSpringWithDamping: 0.88, 
            initialSpringVelocity: 1
        ) {
            self.collectionView.performBatchUpdates {
                updateHandle(self.collectionView)
            }
        }
    }
}
